package com.cxh.rmsq.ui.activity.self

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.cxh.rmsq.R
import com.cxh.rmsq.databinding.ActivityMineTrendsListBinding
import com.cxh.rmsq.ui.activity.TitleBaseActivity
import com.cxh.rmsq.ui.fragment.self.MineTrendsListFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

/**
 * 我的社区、草稿箱
 */
class MineTrendsListActivity : TitleBaseActivity() {
    private lateinit var binding: ActivityMineTrendsListBinding

    private val tabs = arrayOf("已发布", "草稿箱")
    private val mFragments = ArrayList<Fragment>()
    private var mediator: TabLayoutMediator? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMineTrendsListBinding.inflate(layoutInflater)
        setContentView(binding.root)
        titleBar.tvTitle.text = "我的动态"
        mFragments.clear()

        mFragments.add(MineTrendsListFragment.newInstance(1))
        mFragments.add(MineTrendsListFragment.newInstance(2))


        //Adapter
        binding.viewPager.setAdapter(object : FragmentStateAdapter(supportFragmentManager, lifecycle) {
            override fun createFragment(position: Int): Fragment {
                //FragmentStateAdapter内部自己会管理已实例化的fragment对象。
                // 所以不需要考虑复用的问题
                return mFragments[position]
            }

            override fun getItemCount(): Int {
                return mFragments.size
            }
        })
        //viewPager 页面切换监听监听
        //viewPager 页面切换监听监听
        binding.viewPager.registerOnPageChangeCallback(changeCallback)
//        binding.viewPager.setUserInputEnabled(false);
        //        binding.viewPager.setUserInputEnabled(false);
        binding.viewPager.setOffscreenPageLimit(mFragments.size - 1)

        mediator = TabLayoutMediator(
            binding.tabCommunity, binding.viewPager
        ) { tab, position -> //这里可以自定义TabView
            val customView = tab.customView
            if (customView == null) {
                tab.setCustomView(R.layout.tab_item_camp)
            }
            val tab_text = tab.customView!!.findViewById<TextView>(R.id.tab_text)
            val tab_indicator =
                tab.customView!!.findViewById<ImageView>(R.id.tab_indicator)
            if (position < tabs.size) {
                tab_text.text = tabs[position]
            }
            tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19f)
            tab_text.setTypeface(Typeface.DEFAULT_BOLD)
            tab_text.setTextColor(Color.parseColor("#e0000000"))
            tab_indicator.setImageResource(R.mipmap.icon_tab_indicator)
        }
        //要执行这一句才是真正将两者绑定起来
        mediator!!.attach()
    }


    private val changeCallback: OnPageChangeCallback = object : OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            //可以来设置选中时tab的大小
            val tabCount: Int = binding.tabCommunity.getTabCount()
            for (i in 0 until tabCount) {
                val tab: TabLayout.Tab = binding.tabCommunity.getTabAt(i)!!
                if (tab != null) {
                    val customView = tab.customView
                    if (customView != null) {
                        val tab_text = tab.customView!!.findViewById<TextView>(R.id.tab_text)
                        val tab_indicator =
                            tab.customView!!.findViewById<ImageView>(R.id.tab_indicator)
                        //                        if (position == 0) {
//                            tab_text.setTextColor(Color.parseColor("#e0000000"));
//                            tab_indicator.setImageResource(R.mipmap.icon_tab_indicator);
//                        } else if (position == 1) {
//                            tab_text.setTextColor(Color.parseColor("#e0000000"));
//                            tab_indicator.setImageResource(R.mipmap.icon_tab_indicator);
//                        } else if (position == 2) {
//                            tab_text.setTextColor(Color.parseColor("#e0000000"));
//                            tab_indicator.setImageResource(R.mipmap.icon_tab_indicator);
//                        }else if (position == 3) {
//                            tab_text.setTextColor(Color.parseColor("#e0000000"));
//                            tab_indicator.setImageResource(R.mipmap.icon_tab_indicator);
//                        }
                        if (tab.position == position) {
                            tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19f)
                            tab_text.setTypeface(Typeface.DEFAULT_BOLD)
                            tab_indicator.visibility = View.VISIBLE
                            tab_text.setTextColor(Color.parseColor("#e0000000"))
                        } else {
                            tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15f)
                            tab_text.setTypeface(Typeface.DEFAULT)
                            tab_indicator.visibility = View.INVISIBLE
                            tab_text.setTextColor(Color.parseColor("#666666"))
                        }
                    }
                }
            }
        }
    }
}